package com.wolfking.demo.nio.file;

import java.io.IOException;
import java.io.InputStream;
import java.nio.channels.Channels;
import java.nio.channels.FileChannel;
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.stream.Stream;

public class FilesDemo {

    public static void main(String[] args) throws Exception {
        readBytes();
        visitTree();
    }


    private static void readBytes() throws Exception {
        System.out.println(Files.readAllBytes(Paths.get("C:\\ssl_key\\ssl.log")).length);
        FileChannel open = FileChannel.open(Paths.get("C:\\ssl_key\\ssl.log"), StandardOpenOption.APPEND);
        InputStream inputStream = Channels.newInputStream(open);
        System.out.println(inputStream);
    }

    private static void visit() throws Exception {
        Stream<Path> walk = Files.walk(Paths.get("H:\\"));
        walk.forEach(System.out::println);
    }

    private static void visitTree() throws Exception {
        Files.walkFileTree(Paths.get("C:\\"), new SimpleFileVisitor<Path>() {
            @Override
            public FileVisitResult visitFile(Path path,
                                             BasicFileAttributes attrs) throws IOException {
                System.out.println(path);
                return FileVisitResult.CONTINUE;
            }

            @Override
            public FileVisitResult visitFileFailed(Path file, IOException exc)
                    throws IOException {
                return FileVisitResult.CONTINUE;
            }
        });
    }
}
